package com.jiang.daily.Q2209;

/**
 * @author Jiang
 * @version 1.0
 * @date 2025/02/21 13:36
 */
class Solution {
    static final int INF = 0x3f3f3f3f;

    public int minimumWhiteTiles(String floor, int numCarpets, int carpetLen) {
        int n = floor.length();
        int[][] dp = new int[n + 1][numCarpets + 1];
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= numCarpets; j++) {
                dp[i][j] = INF;
            }
        }

        for (int j = 0; j <= numCarpets; j++) {
            dp[0][j] = 0;
        }

        for (int i = 1; i <= n; i++) {
            dp[i][0] = dp[i - 1][0] + (floor.charAt(i - 1) == '1' ? 1 : 0);
        }

        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= numCarpets; j++) {
                dp[i][j] = dp[i - 1][j] + (floor.charAt(i - 1) == '1' ? 1 : 0);
                dp[i][j] = Math.min(dp[i][j], dp[Math.max(0, i - carpetLen)][j - 1]);
            }
        }

        return dp[n][numCarpets];
    }
}